#include<vector>
#include<iostream>
#include<cmath>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


// 在112的基础上改一点就好了。
class Solution {
public:
    vector<int> pathsum;

    void dfs(TreeNode* root, int cur){
        if (!root) return;

        if (!root->left && !root->right) {
            pathsum.push_back(cur);
            return;
        }

        if (root->left) {
            dfs(root->left, cur * 10 + root->left->val);
        }
        if (root->right) {
            dfs(root->right, cur * 10 + root->right->val);
        }
    }
    int sumNumbers(TreeNode* root) {
        if (!root) return false;
        dfs(root, root->val);

        int ans = 0;
        for (auto &x: pathsum){
            ans += x;
        }
        return ans;
    }
};